<template>
  <div>
    <el-row>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        @selection-change="handleSelectionChange"
        :isShowSelection="true"
      >
      </data-table>
    </el-row>

    <el-form
      :inline="true"
      ref="fromdata"
      :model="fromdata"
      :rules="rules"
      label-width="90px"
      label-position="right"
    >
      <el-row>
        <el-col :span="6">
          <el-form-item label="返工类型:" prop="type">
            <el-select
              clearable
              style="width: 200px"
              v-model="fromdata.type"
              placeholder="返工类型"
            >
              <el-option
                v-for="item in dict['返工类型']"
                :key="item.index"
                :value="item.value"
                :label="item.text"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="返工原因:" prop="remark">
            <el-input
              type="textarea"
              :rows="6"
              v-model="fromdata.reworkDesc"
              style="width: 600px"
            >
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row>
      <el-col align="end"
        ><el-button type="success" @click="save">确认返工</el-button></el-col
      >
    </el-row>
  </div>
</template>

<script>
import baseMixin from "@/mixins/base";
import { getDict } from "@/utils";

export default {
  name: "carInfo",
  components: {},
  props: {
    workOrderId: {
      type: Number | String,
    },
    status: {
      type: String,
    },
  }, // workOrderType 工单类型 1快速单
  mixins: [
    baseMixin({
      dataName: ["content", "totalRecords"],
      idName: "id",
    }),
  ],
  computed: {},
  data() {
    return {
      configOption: {}, //配置
      fromdata: {
        id: this.workOrderId,
        projectIds: "", // 项目ids
        type: ""
      },
      tableColumnAttributes: [
        { name: "项目名", value: "projectName", type: "text" },
        { name: "项目工时", value: "projectHours", type: "text" },
        { name: "项目类型", value: "projectType", type: "text" },
        { name: "收费类型", value: "tollType", type: "text" },
        { name: "作业人员/组", value: "workUserName", type: "text" },
        { name: "状态", value: "status", type: "text" },
      ],
      dict: [],
      rules: {
        type: [{ required: true, message: "请选择返工类型", trigger: "change" }],
      },
    };
  },
  async created() {
    this.dict = getDict(["返工类型"]);
    if (this.status === '作业完成') {
      this.fromdata.type = "外部返工";
    } else {
      this.fromdata.type = "内部返工";
    }
    this.getParentConfigOption(this.$parent);
    this.searchOptions[
      `${this.configOption.orderReworkList.queryField}`
    ] = this.workOrderId;
    this.searchOptions.status = '作业完成';
    this.query();
  },
  methods: {
    //获取父级配置
    async getParentConfigOption(dom) {
      if ("getConfig" in dom) {
        this.configOption = dom.getConfig;
        return;
      } else if ("$parent" in dom) {
        this.getParentConfigOption(dom.$parent);
      } else {
        return;
      }
    },
    async query() {
      let f = this.configOption.orderReworkList.query;
      if (f) {
        let res = await f(this.searchOptions, this.pageSize, this.currentPage);
        this.totalCount = parseInt(res.data["totalRecords"]);
        this.tableData = res.data["content"];
      }
    },
    async save() {
      if (this.fromdata.projectIds === "") {
        this.$message.error("请选择一条项目数据");
      }
      this.$refs["fromdata"].validate((valid) => {
        if (valid) {
          let f = this.configOption.orderReworkSave.save;
          if (f) {
            f(this.fromdata).then((res) => {
              let { success } = res;
              if (success) {
                this.$message.success("操作成功");
                this.$emit("reworkResult", true);
              } else {
                this.$message.error("系统错误");
              }
            });
          }
        }
      });
    },
    handleSelectionChange(value) {
      this.fromdata.projectIds = "";
      let select = [];
      value.forEach((element) => {
        select.push(element.carProjectId);
      });
      this.fromdata.projectIds = select.join(",");
    },
  },
  mounted() {},
};
</script>

<style lang="scss" scoped>
</style>
